package com.yunquan.ohana.ui.history.status.decoration

import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yunquan.ohana.R
import com.yunquan.ohana.ui.history.status.adapter.StatusAdapter
import com.yunquan.ohana.utils.Utils

class StatusDecoration(val context: Context) : RecyclerView.ItemDecoration() {
    private var currentHeaderView: View? = null
    private val paint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val subTextPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val textRect = Rect()
    private val subTextRect = Rect()
    private val textPaddingLeft = Utils.instance.dp2px(28f)
    private var mItemHeaderHeight = Utils.instance.dp2px(48f)

    private val receivedBitMap = BitmapFactory.decodeResource(context.resources, R.mipmap.ic_status_received)
    private val unreceivedBitmap = BitmapFactory.decodeResource(context.resources, R.mipmap.ic_status_unreceived)

    init {
        paint.color = context.getColor(R.color.background)
        textPaint.color = context.getColor(R.color.c_14)
        textPaint.textSize = Utils.instance.sp2px(18f).toFloat()
        val typeFace = ResourcesCompat.getFont(context, R.font.montserrat_medium)
        textPaint.typeface = typeFace

        subTextPaint.color = context.getColor(R.color.c_ff8b8b8b)
        subTextPaint.textSize = Utils.instance.sp2px(12f).toFloat()
        val subTypeFace = ResourcesCompat.getFont(context, R.font.montserrat_regular)
        subTextPaint.typeface = subTypeFace
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        if (parent.adapter is StatusAdapter) {
            val adapter = parent.adapter as StatusAdapter
            val childCount = parent.childCount
            for (i in 0 until childCount) {
                val child = parent.getChildAt(i)
                val position = parent.getChildAdapterPosition(child)
                val isHeader = adapter.isGroupHeader(position)
                val left = parent.paddingLeft
                val right = parent.width - parent.paddingRight
                if (isHeader) {
                    c.drawRect(left.toFloat(), (child.top - mItemHeaderHeight).toFloat(), right.toFloat(), child.top.toFloat(), paint)
                    val online = adapter.getItemData(position).receipt ?: false
                    if (online) {
                        val text = parent.context.getString(R.string.received_device)
                        textPaint.color = context.getColor(R.color.c_14)
                        textPaint.getTextBounds(text, 0, text.length, textRect)
                        c.drawText(
                            text,
                            left.toFloat() + textPaddingLeft,
                            (child.top - mItemHeaderHeight / 2 + textRect.height() / 2).toFloat(),
                            textPaint
                        )

                        c.drawBitmap(
                            receivedBitMap,
                            left.toFloat() + textPaddingLeft + textRect.width() + 20,
                            (child.top - mItemHeaderHeight / 2 + receivedBitMap.height / 2 - textRect.height()).toFloat(),
                            textPaint
                        )
                    } else {
                        val subText = context.getString(R.string.tip_unreceived_device)
                        subTextPaint.getTextBounds(subText, 0, subText.length, subTextRect)
                        c.drawText(
                            subText,
                            left.toFloat() + textPaddingLeft,
                            (child.top - mItemHeaderHeight / 3 + subTextRect.height() / 2).toFloat(),
                            subTextPaint
                        )
                        val text = parent.context.getString(R.string.unreceived_device)
                        textPaint.color = context.getColor(R.color.c_ffff3535)
                        textPaint.getTextBounds(text, 0, text.length, textRect)
                        c.drawText(
                            text,
                            left.toFloat() + textPaddingLeft,
                            (child.top - (mItemHeaderHeight / 2 + subTextRect.height() / 3 * 2) + textRect.height() / 2).toFloat(),
                            textPaint
                        )

                        c.drawBitmap(
                            unreceivedBitmap,
                            left.toFloat() + textPaddingLeft + textRect.width() + 20,
                            (child.top - (mItemHeaderHeight / 2 + subTextRect.height() / 3 * 2) + unreceivedBitmap.height / 2 - textRect.height()).toFloat(),
                            textPaint
                        )
                    }
                }
            }
        }
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
        val childCount = parent.childCount
        if (childCount == 0) return

        if (parent.adapter is StatusAdapter) {
            val adapter = parent.adapter as StatusAdapter
            val position = (parent.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
            val view = parent.findViewHolderForAdapterPosition(position)?.itemView
            val isHeader = adapter.isGroupHeader(position + 1)
            val top = parent.paddingTop
            val left = parent.paddingLeft
            val right = parent.width - parent.paddingRight
            if (isHeader) {
                val bottom = mItemHeaderHeight.coerceAtMost(view!!.bottom)
                c.drawRect(
                    left.toFloat(),
                    (top + view.top - mItemHeaderHeight).toFloat(),
                    right.toFloat(),
                    (top + bottom).toFloat(),
                    paint
                )
                val online = adapter.getItemData(position).receipt ?: false
                if (online) {
                    val text = parent.context.getString(R.string.received_device)
                    textPaint.color = context.getColor(R.color.c_14)
                    textPaint.getTextBounds(text, 0, text.length, textRect)
                    c.drawText(
                        text,
                        left.toFloat() + textPaddingLeft,
                        (top + mItemHeaderHeight / 2 + textRect.height() / 2 - (mItemHeaderHeight - bottom)).toFloat(),
                        textPaint
                    )

                    c.drawBitmap(
                        receivedBitMap,
                        left.toFloat() + textPaddingLeft + textRect.width() + 20,
                        (top + mItemHeaderHeight / 2 + receivedBitMap.height / 2 - textRect.height() - (mItemHeaderHeight - bottom)).toFloat(),
                        textPaint
                    )
                } else {
                    val subText = context.getString(R.string.tip_unreceived_device)
                    subTextPaint.getTextBounds(subText, 0, subText.length, subTextRect)
                    c.drawText(
                        subText,
                        left.toFloat() + textPaddingLeft,
                        (top + mItemHeaderHeight / 3 * 2 + subTextRect.height() / 2 - (mItemHeaderHeight - bottom)).toFloat(),
                        subTextPaint
                    )
                    val text = parent.context.getString(R.string.unreceived_device)
                    textPaint.color = context.getColor(R.color.c_ffff3535)
                    textPaint.getTextBounds(text, 0, text.length, textRect)
                    c.drawText(
                        text,
                        left.toFloat() + textPaddingLeft,
                        (top + mItemHeaderHeight / 3 + textRect.height() / 2 - (mItemHeaderHeight - bottom)).toFloat(),
                        textPaint
                    )

                    c.drawBitmap(
                        unreceivedBitmap,
                        left.toFloat() + textPaddingLeft + textRect.width() + 20,
                        (top + mItemHeaderHeight / 3 + unreceivedBitmap.height / 2 - textRect.height() - (mItemHeaderHeight - bottom)).toFloat(),
                        textPaint
                    )
                }
            } else {
                c.drawRect(
                    left.toFloat(),
                    top.toFloat(),
                    right.toFloat(),
                    (top + mItemHeaderHeight).toFloat(),
                    paint
                )
                val online = adapter.getItemData(position).online ?: false
                if (online) {
                    val text = parent.context.getString(R.string.received_device)
                    textPaint.color = context.getColor(R.color.c_14)
                    textPaint.getTextBounds(text, 0, text.length, textRect)
                    c.drawText(
                        text,
                        left.toFloat() + textPaddingLeft,
                        (top + mItemHeaderHeight / 2 + textRect.height() / 2).toFloat(),
                        textPaint
                    )

                    c.drawBitmap(
                        receivedBitMap,
                        left.toFloat() + textPaddingLeft + textRect.width() + 20,
                        (top + mItemHeaderHeight / 2 + receivedBitMap.height / 2 - textRect.height()).toFloat(),
                        textPaint
                    )
                } else {
                    val subText = context.getString(R.string.tip_unreceived_device)
                    subTextPaint.getTextBounds(subText, 0, subText.length, subTextRect)
                    c.drawText(
                        subText,
                        left.toFloat() + textPaddingLeft,
                        (top + mItemHeaderHeight / 3 * 2 + subTextRect.height() / 2).toFloat(),
                        subTextPaint
                    )
                    val text = parent.context.getString(R.string.unreceived_device)
                    textPaint.color = context.getColor(R.color.c_ffff3535)
                    textPaint.getTextBounds(text, 0, text.length, textRect)
                    c.drawText(
                        text,
                        left.toFloat() + textPaddingLeft,
                        (top + mItemHeaderHeight / 3 + textRect.height() / 2).toFloat(),
                        textPaint
                    )

                    c.drawBitmap(
                        unreceivedBitmap,
                        left.toFloat() + textPaddingLeft + textRect.width() + 20,
                        (top + mItemHeaderHeight / 3 + unreceivedBitmap.height / 2 - textRect.height()).toFloat(),
                        textPaint
                    )
                }
            }
            c.save()
        }
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        if (parent.adapter is StatusAdapter) {
            val adapter = parent.adapter as StatusAdapter
            val position = parent.getChildLayoutPosition(view)
            val isHeader = adapter.isGroupHeader(position)
            if (isHeader) {
                outRect.top = mItemHeaderHeight
            }
        }
    }
}